﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ERPStore.Extensions;
using ERPStore.Web.Extensions;

namespace ERPStore.Web.Logging
{
	public class WebError
	{
		public static void Handle(System.Web.HttpContext ctx)
		{
			// Code that runs when an unhandled error occurs
			if (ctx.Request.IsLocal)
			{
				return;
			}

			Exception ex = null;
			try
			{
				ctx.Server.GetLastERPStoreException(ctx);
			}
			catch
			{
				ex = ctx.Server.GetLastError();
			}

			if (ex == null)
			{
				return;
			}

			var logger = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Logging.ILogger>();
			logger.Fatal(ex);
		}
	}
}
